package com.jfinal.bbsapp.fp;
import java.util.List;
import org.apache.log4j.Logger;
import com.jfinal.bbsapp.common.kit.MyAnnotation;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@MyAnnotation(memo="app首页业务类")
public class FpService {
	private final Logger logger = Logger.getLogger(FpService.class);
	
	@MyAnnotation(memo="获取启动图")
	public Ret getwx() {
		try {
			Record rec = Db.findFirst("select wxname,img from a_wx");
			return Ret.ok("data", rec).set("code", 20000);
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取轮转图")
	public Ret bannerlist(String bannertype) {
		List<Record> list = null;
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("select cast(bannerid as char) bannerid,bannername,img,sortno");
			sql.append(" from a_banner");
			sql.append(" where flag <> '9'");
			if(StrKit.notBlank(bannertype)) {
				sql.append(" and bannertype='" + bannertype + "'");
			}
			sql.append(" order by sortno asc");
			list = Db.find(sql.toString());
			return Ret.ok("data", list).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取课程分类")
	public Ret courselist() {
		List<Record> list = null;
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("select cast(courseid as char) courseid,coursename,img,classhour,ifnull(annex,'') annex,memo,sortno");
			sql.append(" from c_course where flag='1' and isfree='0'");
			sql.append(" order by sortno asc");
			list = Db.find(sql.toString());
			return Ret.ok("data", list).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取免费试听课程")
	public Ret freelist() {
		List<Record> list = null;
		try {
			StringBuilder sql = new StringBuilder();
			sql.append("select cast(chapterid as char) chapterid,chaptername,img,videourl,sortno");
			sql.append(" from c_chapter where isfree='1'");
			sql.append(" order by sortno asc limit 6");
			list = Db.find(sql.toString());
			return Ret.ok("data", list).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="更多免费试听课程分页列表")
	public Ret freeplist(String token,int pagecurrent, int pagesize) {
		try {
			String querySql = "select cast(chapterid as char) chapterid,chaptername,img,videourl,sortno";	
			StringBuilder sql = new StringBuilder();
			sql.append(" from c_chapter");
			sql.append(" where isfree='1'");
			sql.append(" order by sortno asc");
			Page<Record> page = Db.paginate(pagecurrent, pagesize, querySql, sql.toString());
			return Ret.ok("data", page).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
}